let express =require("express");
let server = express();
let mysql = require("mysql");
let ejs = require("ejs");

let sql = mysql.createConnection({
  host     : 'localhost',
  user     : 'root',
  password : '000000',
  database : 'conpany',
  port     :  3305
});
// 尝试链接
sql.connect();

server.use(express.static(__dirname + '/public'));

server.get("/solvelist",function(request,response){
	sql.query(`select * from solve`,function(error,data){
		if(error){
			console.log(error);
			return;
		}
		ejs.renderFile("solveList.html",{solvelist:data},function(error,result){
			if(error){
				console.log(error)
				return;
			}
			response.end(result)
		});
	})
})

server.get("/solveinfo",(request,response)=>{
	let id=request.query.id;
	sql.query(`SELECT *from solve where id=${id}`,(error,data)=>{
		if(error){
			console.log(error)
			return;
		}

		ejs.renderFile("solveInfo.html",{solveinfolist:data[0]}, function(error,result){
			if(error){
				console.log(error)
				return;
			}
			//console.log(result)
			response.end(result)
		});
	})
})


server.get("/caselist",function(request,response){
	sql.query(`select * from casetable`,function(error,data){
		if(error){
			console.log(error);
			return;
		}
		ejs.renderFile("caseList.html",{caselist:data},function(error,result){
			if(error){
				console.log(error)
				return;
			}
			response.end(result)
		});
	})
})

server.get("/caseinfo",(request,response)=>{
	let id=request.query.id;
	sql.query(`SELECT * from casetable where id=${id}`,(error,data)=>{
		if(error){
			console.log(error)
			return;
		}

		ejs.renderFile("caseInfo.html",{caseinfolist:data[0]}, function(error,result){
			if(error){
				console.log(error)
				return;
			}
			//console.log(result)
			response.end(result)
		});
	})
})
server.listen(8080)